// 定义变量pwmDutyCycle并初始化为0，用于存储PWM占空比
float pwmDutyCycle = 0;
// 定义常量，表示PWM输出引脚
const int pwmOutputPin = 5;
// 定义常量，表示GND输出引脚
const int pwmGndPin = 18;
// 定义常量，设定功率差值阈值，允许一定误差
const float powerDiffThreshold = 2;
// 定义整形，并网循环时间
int gridCycleTime = 500;
// 定义布尔，并网启动标志
bool grdOnOff = false;

// 调整PWM占空比的函数
void adjustPwm(float usagePower, float gridPower)
{
    float pwmAdjust = 0;
    // 计算用电功率与并网功率的差值的绝对值，得到功率差值powerDiff
    float powerDiff = abs(gridPower + powerDiffThreshold - usagePower);
    // 当用电功率大于并网功率，并且功率差值大于设定的功率差值阈值时
    if (usagePower > gridPower + powerDiffThreshold)
    {
        // 将功率差值转换为PWM调整值
        if (usagePower > 50)
        {
            pwmAdjust = powerDiff;
        }
        else
        {
            pwmAdjust = powerDiff / 2;
        }
        // 当调整后的PWM占空比加上当前PWM占空比小于4095（12位分辨率下PWM占空比的最大值 ）时
        if (pwmDutyCycle + pwmAdjust <= 4095)
        {
            // 更新PWM占空比
            pwmDutyCycle += pwmAdjust;
        }
        else if (pwmDutyCycle + pwmAdjust / 3 <= 4095)
        {
            // 更新PWM占空比
            pwmDutyCycle += pwmAdjust / 3;
        }
    }
    // 当用电功率小于并网功率时
    else if (usagePower < gridPower + powerDiffThreshold)
    {
        // 将功率差值转换为PWM调整值
        if (usagePower > 50 || usagePower < gridPower)
        {
            pwmAdjust = powerDiff * 3;
        }
        else
        {
            pwmAdjust = powerDiff / 2;
        }
        // 当当前PWM占空比减去调整值大于0时
        if (pwmDutyCycle - pwmAdjust >= 0)
        {
            // 更新PWM占空比
            pwmDutyCycle -= pwmAdjust;
        }
    }
    // 这里假设ledcWrite函数已经正确定义并能设置PWM占空比
    ledcWrite(0, int(pwmDutyCycle));

    // 打印当前的PWM占空比数值
    // Serial.print("占空比: ");
    // Serial.println(pwmDutyCycle);
}
// 循环处理并网逻辑
inline void loopGrid()
{
    static bool firstRun = true;
    static long loopGridTick = 0;
    if (firstRun)
    {
        firstRun = false;
        ledcSetup(0, 10000, 12);        // PWM初始化
        ledcAttachPin(pwmOutputPin, 0); // 绑定PWM通道
        ledcWrite(0, 0);                // PWM占空比初始化
        pinMode(pwmGndPin, OUTPUT);     // PWM占空比GND输出
        digitalWrite(pwmGndPin, LOW);   // PWM占空比GND输出
    }
    if (millis() - loopGridTick > gridCycleTime && grdOnOff)
    {
        if (config.switchArray[0] == 1)
        {
            // 假设的用电功率和并网功率，实际应根据实际情况获取
            adjustPwm(usagePower, gridPower);
            gridEfficiency = gridPower / batteryPower;
            if (usagePower > powerDiffThreshold)
            {
                DataPreTick = 100;
            }
            else
            {
                DataPreTick = 2000;
            }

            if (usagePower > 50)
            {
                gridCycleTime = 80;
            }
            else
            {
                gridCycleTime = 1000;
            }
            loopGridTick = millis();
        }
        else
        {
            ledcWrite(0, 0);
            pwmDutyCycle = 0;
            DataPreTick = 2000;
            gridCycleTime = 1000;
            gridEfficiency = 0;
        }



    }
}